package pers.qianyu.month_202011.date_20201128;

import java.util.*;

/**
 * [Leetcode 每日一题]
 * 454. 四数相加 II
 * https://leetcode-cn.com/problems/4sum-ii/
 *
 * @author mizzle rain
 * @date 2020-11-28 13:10
 */
public class FourSumCount {
    public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int a : A) {
            for (int b : B) {
                int sum = a + b;
                map.put(sum, map.getOrDefault(sum, 0) + 1);
            }
        }
        int res = 0;
        for (int c : C) {
            for (int d : D) {
                int sum = c + d;
                if (map.containsKey(-sum)) {
                    res += map.get(-sum);
                }
            }
        }
        return res;
    }
}
